package com.pink.entity.vo;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

public record RestBeanVo<T>(int code, T data, String message) {

    public static <T> RestBeanVo<T> success(T data) {
        return new RestBeanVo<>(200,data,"请求成功");
    }

    public static <T> RestBeanVo<T> success() {
        return success(null);
    }

    public static <T> RestBeanVo<T> failure(String message) {
        return new RestBeanVo<>(401,null,message);
    }

    public static <T> RestBeanVo<T> forbidden(String message) {
        return new RestBeanVo<>(403,null,message);
    }

    public static <T> RestBeanVo<T> failure(int code, String message) {
        return new RestBeanVo<>(code,null,message);
    }

    public String asJsonString() {
        return JSONObject.toJSONString(this,JSONWriter.Feature.WriteNulls);
    }
}
